//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef _EVT_MANAGER_YG_H__
#define _EVT_MANAGER_YG_H__

#include "def.h"

enum cRCEVENTID
{
	cRCEventNone=0,

	//Session
	cRCEventSessionBeforeConnect,
	cRCEventSessionAfterConnect,
	cRCEventSessionConnectFailed,
	cRCEventSessionUp,
	cRCEventSessionDown,

	//Perfomance
	cRCEventNAT,
	cRCEventRT,
	cRCEventMemory,
	cRCEventCPU,

	//Conntrack
	cRCEventConntrackNew,
	cRCEventConntrackRemove,

	//Syslog
	cRCEventSysLog,

	//Access
	cRCEventLanConnect,
	cRCEventLanDisConnect,
	cRCEventWirelessConnect,
	cRCEventWirelessDisConnect,


};

enum cRCEVENTFields
{
	cRCEventFieldRawData=0,
	cRCEventFieldMAC,
	cRCEventFieldIP,
	cRCEventFieldTTL,
	cRCEventFieldState,
	cRCEventFieldProtocol,
	cRCEventFieldEventTime,
	cRCEventCurResourceLevel,
	cRCEventMaxResourceLevel,
	cRCEventResourceUsageLevel,
};

enum cRCActions
{
	cRCActionLog			= 1<<0,
	cRCActionRun			= 1<<1,
	cRCActionSendToRouterCLI= 1<<2,
	cRCActionSendToHTTP		= 1<<3,
};

typedef struct tagEVENTACTION
{
	//key
	cRCEVENTID		cEventID;
	unsigned int	iActions;		

	FILTER			filter;
	wxString		strLogMSG;
	wxString		strRUNPath;
	wxString		strARGList;
	wxString		strSendToRouter;
}EVENTACTION;
typedef std::vector<EVENTACTION> EVENTACTIONLIST;

typedef struct tagRCEVENT
{
	tagRCEVENT():cEventID(cRCEventNone), stTTL(0), stCurLevel(0), stMAXLevel(0)
	{
		tEvent = time(NULL);
	}

	//key
	cRCEVENTID  cEventID;

	wxString	strRawEvent;

	wxString	strMAC;
	wxString	strIP;
	wxString	strState;
	wxString	strProtocol;
	time_t		tEvent;

	size_t		stTTL;
	size_t		stCurLevel;
	size_t		stMAXLevel;

}RCEVENT;


class CEventManager
{
public:
								CEventManager(void);
								~CEventManager(void);

	void						AddEvent(const RCEVENT& evnt);
	void						ResroreDefaultActions();

protected:
	void						CheckAction(const EVENTACTION& action, const RCEVENT& evnt);
	bool						Match(const FIELDRULESET& rlSet, const SEARCHFIELD& sf, const wxString& strVal) const;
	bool						ApplyRule(const FILTERFIELDRULE& rule, const SEARCHFIELD& sf, const wxString& strVal) const;


	void						ExecuteAction(const EVENTACTION& action, const RCEVENT& evnt);
	wxString					FieldToString( int column, const RCEVENT& evnt);


	void						LogAction(const EVENTACTION& action, const RCEVENT& evnt);
	void						RunAction(const EVENTACTION& action, const RCEVENT& evnt);
	void						SendToRouterAction(const EVENTACTION& action, const RCEVENT& evnt);
	void						SendToHTTPAction(const EVENTACTION& action, const RCEVENT& evnt);


private:
	EVENTACTIONLIST				m_actions;
};

#endif //_EVT_MANAGER_YG_H__